home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 February: Tool Chest / Dev.CD Feb 95 / Dev.CD Feb 95.toast / Tool Chest / QuickDraw GX / QuickDraw GX Info / QuickDraw GX Interfaces / Interfaces & Libraries / graphics libraries / graphicsBug library.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-05  |  8.2 KB  |  209 lines  |  [TEXT/MPS ]

  1. /* graphics libraries:
  2.     general library routines    
  3.     by Cary Clark, Georgiann Delaney, Michael Fairman, Dave Good, Robert Johnson, Keith McGreggor, Oliver Steele, David Van Brink, Chris Yerga
  4.     Copyright 1987 - 1993 Apple Computer, Inc.  All rights reserved.    */
  5.  
  6. /* this is so that we get the trap version of Gestalt */
  7. #ifndef SystemSevenOrLater
  8.     #define SystemSevenOrLater 1
  9. #endif
  10.     
  11. #ifdef THINK_C
  12.     #include <MacHeaders>
  13. #endif
  14. #include <GestaltEqu.h>
  15. #include <Processes.h>
  16. #include "math types.h"
  17. #include "graphics debugging.h"
  18. #include "graphicsBug library.h"
  19.  
  20.  
  21. // *** eventually, allow this to work both locally and over the network?
  22. // *** for remote debugging, lauch gBug on remote machine, and then use special menu item
  23. // to bring up PPCToolbox browser. Select one or more machines running graphics init to receive 
  24. // errors from, or to set break points, etc.
  25.  
  26. boolean SendMessageToGraphicsBug(long command, const char *string, unsigned long message, boolean waitForCompletion)
  27. {
  28.     ProcessSerialNumber     graphicsBugProcess;
  29.     OSErr               err;
  30.     struct graphicsBugParameters *blockPointer = nil;
  31.     EventRecord         theEvent;
  32.     
  33.     {   LaunchParamBlockRec     launch;
  34.         DTPBRec             bugAp;
  35.         FSSpec              fileSpecification;
  36.         
  37.         bugAp.ioNamePtr = nil;
  38.         bugAp.ioVRefNum = 0;
  39.         if ((err = PBDTGetPath(&bugAp)) != 0)
  40.             return false; /* error */
  41.         bugAp.ioNamePtr = fileSpecification.name;
  42.         bugAp.ioCompletion = nil;
  43.         bugAp.ioIndex = 0;
  44.         bugAp.ioFileCreator = creatorType;
  45.         err = PBDTGetAPPLSync(&bugAp);
  46.         if (err)
  47.             return false; /* error */
  48.         /* is the file already launched ? */
  49.         {   ProcessInfoRec processInfo;
  50.             FSSpec processSpec;
  51.             
  52.             processInfo.processName = nil;
  53.             processInfo.processAppSpec = &processSpec;
  54.             processInfo.processInfoLength = sizeof(processInfo);
  55.             graphicsBugProcess.highLongOfPSN = 0;
  56.             graphicsBugProcess.lowLongOfPSN = kNoProcess;
  57.             while (GetNextProcess(&graphicsBugProcess) == noErr) {
  58.                 if (GetProcessInformation(&graphicsBugProcess, &processInfo) == noErr &&
  59.                     processInfo.processType == 'APPL' &&
  60.                     processInfo.processSignature == creatorType &&
  61.                     processInfo.processAppSpec->parID == bugAp.ioAPPLParID)
  62.                 {
  63.                     goto doEvent;
  64.                 }
  65.             }
  66.         }
  67.         launch.launchBlockID = extendedBlock;
  68.         launch.launchEPBLength = extendedBlockLen;
  69.         launch.launchFileFlags = 0;
  70.         launch.launchControlFlags = launchDontSwitch | launchContinue | launchNoFileFlags;
  71.         fileSpecification.vRefNum = 0;
  72.         fileSpecification.parID = bugAp.ioAPPLParID;
  73.         launch.launchAppSpec = &fileSpecification;
  74.         launch.launchAppParameters = nil;
  75.         err = LaunchApplication(&launch);
  76.         if (err)
  77.             return false; /* error */
  78.         if (launch.launchMinimumSize)  /* ap not already open */
  79.         {   register short counter = 4;
  80.         
  81.             do
  82.                 WaitNextEvent(0, &theEvent, -1, nil);
  83.             while (--counter);
  84.         }
  85.         graphicsBugProcess = launch.launchProcessSN;
  86.     }
  87. doEvent:
  88. #ifdef __APPLEEVENTS__
  89.     {   AEAddressDesc   targetAddress;
  90.         AppleEvent  theAppleEvent;
  91.         AppleEvent  replyEvent;
  92.     
  93.         err = AECreateDesc(typeProcessSerialNumber, (Ptr)&graphicsBugProcess, sizeof(graphicsBugProcess), &targetAddress);
  94.         err = AECreateAppleEvent(kCoreEventClass, kAEOpenApplication, &targetAddress, kAutoGenerateReturnID,
  95.             kAnyTransactionID, &theAppleEvent);
  96.         err = AESend(&theAppleEvent, &replyEvent, kAENoReply | kAECanInteract | kAECanSwitchLayer, kAENormalPriority, kAEDefaultTimeout, nil, nil);
  97.         AEDisposeDesc(&theAppleEvent);
  98.         
  99.         //  select the bottom of the window just in case it was pointing to the top
  100.         err = AECreateAppleEvent(bugEventClass, selectBugWindowPartEvent, &targetAddress, kAutoGenerateReturnID,
  101.             kAnyTransactionID, &theAppleEvent);
  102.         {   boolean selectTop = false;
  103.             
  104.             err = AEPutParamPtr(&theAppleEvent, chooseWindowPartBoolean, keyDirectObject, (Ptr)&selectTop, sizeof(selectTop));
  105.         }
  106.         err = AESend(&theAppleEvent, &replyEvent, kAENoReply | kAECanInteract | kAECanSwitchLayer, kAENormalPriority, kAEDefaultTimeout, nil, nil);
  107.         AEDisposeDesc(&theAppleEvent);
  108.         
  109.         // create a second event that contains the error message and paste it into the window
  110.         err = AECreateAppleEvent(bugEventClass, pasteBugTextEvent, &targetAddress, kAutoGenerateReturnID,
  111.             kAnyTransactionID, &theAppleEvent);
  112.         {   Str255 quoted;
  113.             long length = strlen(str);
  114.             unsigned char *quotePtr = quoted;
  115.             
  116.             *quotePtr++ = '"';
  117.             BlockMove(str, quotePtr, length);
  118.             quotePtr += length;
  119.             if (num)
  120.             {   register int digit;
  121.                 register int counter = 8;
  122.                 
  123.                 BlockMove(": 0", quotePtr, 3);
  124.                 quotePtr += 3;
  125.                 *quotePtr++ = 'X';
  126.                 do {
  127.                     num = num << 4 | num >> 28;
  128.                     digit = num & 0xf;
  129.                     *quotePtr++ = digit > '9' ? digit - '9' + 'A' - 1 : digit + '0';
  130.                 } while (--counter);
  131.             }
  132.             *quotePtr++ = '"';
  133.             *quotePtr++ = returnChar;
  134.             err = AEPutParamPtr(&theAppleEvent, errorString, keyDirectObject, (Ptr) quoted, quotePtr - quoted);
  135.         }
  136.         err = AESend(&theAppleEvent, &replyEvent, kAEWaitReply | kAECanInteract | kAECanSwitchLayer, kAENormalPriority, kAEDefaultTimeout, nil, nil);
  137.         AEDisposeDesc(&theAppleEvent);
  138.         AEDisposeDesc(&targetAddress);
  139.     }
  140. #else
  141.     {
  142.         if (GXGetGraphicsBugParametersPointer(&blockPointer))
  143.         {   char lastToggle;
  144.             long idleCount = 0;
  145.             
  146.             while (blockPointer->status <= graphicsBugRunning) {
  147.                 if (lastToggle != blockPointer->toggle) {
  148.                     lastToggle = blockPointer->toggle;
  149.                     idleCount = 0;
  150.                 } else {
  151.                     idleCount++;
  152.                     if (idleCount > maximumIdle)
  153.                         return false; /* timed out */
  154.                 }
  155.                 EventAvail(everyEvent, &theEvent);
  156.             }
  157.             {   register char *destStringPtr = blockPointer->string;
  158.                 register const char *sourceStringPtr = string;
  159.                 static char requestChars[] = "nwe";
  160.                 
  161.                 blockPointer->message = message;
  162.                 if (command >= noticeMessage && command <= errorMessage) {
  163.                     *destStringPtr++ = 'g';
  164.                     *destStringPtr++ = requestChars[command];
  165.                 } else if (sourceStringPtr) {
  166.                     if (command == debugMessage)
  167.                         *destStringPtr++ = '"';
  168.                     while (*sourceStringPtr)
  169.                         *destStringPtr++ = *sourceStringPtr++;
  170.                     if (command == debugMessage)
  171.                         *destStringPtr++ = '"';
  172.                 }
  173.                 blockPointer->command = executeCommand;
  174.             }
  175.             if (waitForCompletion) {
  176.                 while (blockPointer->status) {
  177.                     if (lastToggle != blockPointer->toggle) {
  178.                         lastToggle = blockPointer->toggle;
  179.                         idleCount = 0;
  180.                     } else {
  181.                         idleCount++;
  182.                         if (idleCount > maximumIdle)
  183.                             return false;
  184.                     }
  185.                     EventAvail(everyEvent, &theEvent);
  186.                 }
  187.             }
  188.         } else
  189.             return false;
  190.     }
  191. #endif
  192.     return true;
  193. }
  194.  
  195. static boolean SendGraphicsBugMessage(const char *str, long message, long reference)
  196. {
  197.     reference = 0;  /* so MPW will not complain */
  198.     if (SendMessageToGraphicsBug(debugMessage, str, message, true) == false) {
  199.         GXSetUserGraphicsDebug(nil, 0);
  200.         return false;
  201.     }
  202.     return true;
  203. }
  204.  
  205. void DirectDebugMessageToGraphicsBug(void)
  206. {
  207.     GXSetUserGraphicsDebug(SendGraphicsBugMessage, 0);
  208. }
  209.